package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponMapper;
import com.qf.pojo.DtsCoupon;
import com.qf.service.DtsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class DtsCouponServiceImpl implements DtsCouponService {

    @Autowired
    DtsCouponMapper couponMapper;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param name
     * @param type
     * @param status
     * @return
     */
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String name, String type, String status) {
        IPage<DtsCoupon> iPage = new Page<>(page,limit);
        QueryWrapper<DtsCoupon> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.eq("name",name);
        }
        if (!StringUtils.isEmpty(type)){
            wrapper.eq("type",type);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        couponMapper.selectPage(iPage,wrapper);

        //封装返回值对象
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());

        return data;
    }

    /**
     * 新增
     * @param name
     * @param desc
     * @param tag
     * @param total
     * @param discount
     * @param min
     * @param limit
     * @param type
     */
    @Override
    public void insert(String name, String desc, String tag, Integer total, String discount, Integer min, Integer limit, Integer type) {
        DtsCoupon coupon = new DtsCoupon();
        coupon.setName(name);
        coupon.setDesc(desc);
        coupon.setTag(tag);
        coupon.setTotal(total);
        coupon.setDiscount(new BigDecimal(discount));
        coupon.setMin(new BigDecimal(min));
        coupon.setLimit(limit);
        coupon.setType(type);

        couponMapper.insert(coupon);
    }

    /**
     * 删除
     * @param coupon
     */
    @Override
    public void delete(DtsCoupon coupon) {
        couponMapper.deleteById(coupon.getId());
    }

    /**
     * 更新
     * @param coupon
     */
    @Override
    public void update(DtsCoupon coupon) {
        couponMapper.updateById(coupon);
    }
}
